package com.etgraph.recap.msof.template.creator;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.etgraph.recap.msof.template.ExcelTemplateStyle;
import com.etgraph.recap.msof.template.griditem.RecapItem;

public class SXSSFExcelTemplateCreator implements ExcelTemplateCreator {

	private SXSSFWorkbook wb;
	private Sheet sheet;
	private int rownum = 1;
	private static final int START_CELL_NUM = 0x1;
	private Set<Integer> autoSizeColumns = new HashSet<Integer>();

	public SXSSFExcelTemplateCreator(SXSSFWorkbook wb){
		this.wb = wb;
		sheet = this.wb.createSheet("stand up meeting recapitulate");
	}
	
	public void createDate(String dateText, Date date) {
		Row r = sheet.createRow(rownum);
		rownum ++;
		Cell dateTextCell = r.createCell(START_CELL_NUM);
		dateTextCell.setCellValue(dateText);
		ExcelTemplateStyle style = new ExcelTemplateStyle(wb);
		Cell dateCell = r.createCell(START_CELL_NUM + 1);
		dateCell.setCellStyle(style.appendDateFormate().getCellStyle());
		dateCell.setCellValue(date);
	}

	public void createOutstandingItem(String outstandingItemText, String outstandingItem) {
		Row r = sheet.createRow(rownum);
		rownum ++;
		Cell outstandingItemTextCell = r.createCell(START_CELL_NUM);
		outstandingItemTextCell.setCellValue(outstandingItemText);
		ExcelTemplateStyle style = new ExcelTemplateStyle(wb);
		outstandingItemTextCell.setCellStyle(style.appendFontRead().appendFontBold().getCellStyle());
		Cell outstandingItemCell = r.createCell(START_CELL_NUM + 1);
		outstandingItemCell.setCellValue(outstandingItem);
	}

	public void createRecapItem(Set<RecapItem> item) {
		for (RecapItem recapItem : item) {
			Row r = sheet.createRow(rownum);
			rownum ++;
			Cell nameTextCell = r.createCell(START_CELL_NUM);
			nameTextCell.setCellValue(recapItem.getName());
			Cell downTextCell = r.createCell(START_CELL_NUM + 1);
			downTextCell.setCellValue(recapItem.getDown());
			Cell followUpTextCell = r.createCell(START_CELL_NUM + 2);
			followUpTextCell.setCellValue(recapItem.getFollowUp());
		}
		autoSizeColumns.add(START_CELL_NUM);
		autoSizeColumns.add(START_CELL_NUM + 1);
		autoSizeColumns.add(START_CELL_NUM + 2);
	}
	
	public void createRecapItemTitle(String... titles) {
		int colnum = START_CELL_NUM;
		Row r = sheet.createRow(rownum);
		for (String title : titles) {
			Cell cell = r.createCell(colnum);
			cell.setCellValue(title);
			ExcelTemplateStyle style = new ExcelTemplateStyle(wb);
			cell.setCellStyle(style.appendFillPattern().appendFontWhite().getCellStyle());
			colnum++;
		}
		rownum ++;
	}

	public void createMisc(String miscText, String misc) {
		Row r = sheet.createRow(rownum);
		rownum ++;
		Cell miscTextCell = r.createCell(START_CELL_NUM);
		miscTextCell.setCellValue(miscText);
		ExcelTemplateStyle style = new ExcelTemplateStyle(wb);
		miscTextCell.setCellStyle(style.appendFontBold().getCellStyle());
		Cell cell = r.createCell(START_CELL_NUM + 1);
		cell.setCellValue(misc);
	}
	
	public void autoSizeColumn(){
		ExcelTemplateStyle style = new ExcelTemplateStyle(wb);
		style.setAutoSize(autoSizeColumns, sheet);
	}
}
